/**
 *  列表页
 *
 * 作者 zhangwei
 * 日期 2024-09-05 09:48:17
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-educationCautionaryLesson'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#educationCautionaryLesson-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'educationCautionaryLessonTable',
            url: ctx + 'educationCautionaryLesson/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {
                    field: 'tenantId',
                    title: '公司名称',
                    minWidth: 120,
                    hide: !hasHeadCompanyRole,
                    templet: function (d) {
                        var tenant = tenantList.filter(item => item.tenantId == d.tenantId);
                        return tenant[0].tenantName;
                    }
                },
                {field: 'cautionarySource',align: 'center', title: '警示来源', width: 120,templet:function (d) {
                        let str = "";
                        switch (d.cautionarySource) {
                            case "1":str = "事故管理";break;
                            case "2":str = "违章记分";break;
                            case "3":str = "隐患上报";break;
                            case "4":str = "安全检查";break;
                        }
                        return str;
                    }},
                {field: 'cautionaryTitle', title: '警示标题', minWidth: 250, templet:function (d) {
                    return '<span style="color:#01aaed;cursor:pointer" lay-event="detail">'+ d.cautionaryTitle +'</span>'
                    }},
                {field: 'studyUserNo',align: 'center', title: '员工工号', width: 120},
                {field: 'studyRealName',align: 'center', title: '员工姓名', width: 120},
                {field: 'studyDept',align: 'center', title: '警示部门', width: 120},
                {field: 'studyPostName',align: 'center', title: '员工岗位', width: 120},
                {field: 'studyDate',align: 'center', title: '学习时间', width: 150},
                {field: 'studyAddress',align: 'center', title: '学习地点', minWidth: 140},
                {field: 'status',align: 'center', title: '警示状态', width: 120,templet:function (d) {
                        let str = "";
                        switch (d.status) {
                            case "0":str = '<span class="layui-badge febs-bg-red">待学习</span>';break;
                            case "1":str = '<span class="layui-badge febs-bg-cyan">已学习</span>';break;
                        }
                        return str;
                    }},
                {title: '操作', toolbar: '#educationCautionaryLesson-tool', align: 'center', width: 160}
            ]],
            toolbar: '#educationCautionaryLesson-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(educationCautionaryLessonTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(educationCautionaryLessonTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增', 'educationCautionaryLesson/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '90%' : ['700px','600px'],
                yes: function (index, layero) {
                    $('#educationCautionaryLesson-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: $tenantSelect.val(),
                }
            });
        }
        if(obj.event === 'qdExport'){
            var params = {};
            febs.download(ctx + 'user/excel4SignIn', params, '签到表.xls');
        }
    });

    table.on('tool(educationCautionaryLessonTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        let detailTitle = "";
        let detailUrl = "";
        let detailLessonUrl = "";
        switch (data.cautionarySource) {
            case "1":
                detailTitle = '事故上报详情';
                detailUrl = 'accidentReport/detail/'+data.cautionarySourceId;
                detailLessonUrl = 'accidentReport/detailForLesson/'+data.cautionarySourceId+'/'+data.cautionaryLessonId;
                break;
            case "2":
                detailTitle = '违章信息详情';
                detailUrl = 'peccancyInformation/detail/'+data.cautionarySourceId;
                detailLessonUrl = 'peccancyInformation/detailForLesson/'+data.cautionarySourceId+'/'+data.cautionaryLessonId;
                break;
            case "3":
                detailTitle = '隐患信息详情';
                detailUrl = 'hazardList/detail/'+data.cautionarySourceId;
                detailLessonUrl = 'hazardList/detailForLesson/'+data.cautionarySourceId+'/'+data.cautionaryLessonId;
                break;
            case "4":
                detailTitle = '安全检查详情';
                detailUrl = 'hazardExamine/detail/'+data.cautionarySourceId;
                detailLessonUrl = 'hazardExamine/detailForLesson/'+data.cautionarySourceId+'/'+data.cautionaryLessonId;
                break;
        }
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.cautionaryLessonId);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('学习', detailLessonUrl, {
                area: $(window).width() <= 750 ? '90%' : ['85%','90%'],
                btn: ['学习', '取消'],
                yes: function (index, layero) {
                    $('#educationCautionaryLesson-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: $tenantSelect.val(),
                },
            });
        }
        if(obj.event === 'detail'){
            febs.modal.open(detailTitle, detailUrl, {
                btn: ['关闭'],
                area: $(window).width() <= 750 ?  ['85%', '80%']: ['95%', '95%'],
                shadeClose:true,
                yes: function (index, layero) {
                    layer.closeAll();
                },
            });
        }
    });

    function getQueryParams() {
        return {
            tenantId: $searchForm.find("select[name='tenantSelect']").find("option:selected").val(),
            cautionarySource: $searchForm.find("select[name='cautionarySource']").find("option:selected").val(),
            cautionaryTitle: $searchForm.find('input[name="cautionaryTitle"]').val()?.trim(),
            studyDept: $searchForm.find('input[name="studyDept"]').val()?.trim(),
            studyUserNo: $searchForm.find('input[name="studyUserNo"]').val()?.trim(),
            studyRealName: $searchForm.find('input[name="studyRealName"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'educationCautionaryLesson/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});